home *** CD-ROM | disk | FTP | other *** search
- Path: camelot.dsccc.com!not-for-mail
- From: kcline@sun152.spd.dsccc.com (Kevin Cline)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ & macros
- Date: 28 Feb 1996 18:27:16 -0600
- Organization: DSC Communications Corporation Switch Products Division
- Message-ID: <4h2rt4$4cg@sun152.spd.dsccc.com>
- References: <DLBxoH.GLw@ariel.cs.yorku.ca> <4gg06d$401m@news-s01.ny.us.ibm.net> <4gioa2$nsb@sun152.spd.dsccc.com> <4h0h4o$7a3@xanadu.io.com>
- NNTP-Posting-Host: sun152.spd.dsccc.com
-
- In article <4h0h4o$7a3@xanadu.io.com>, Jamshid Afshar <jamshid@io.com> wrote:
- >In article <4gioa2$nsb@sun152.spd.dsccc.com>,
- >Kevin Cline <kcline@sun152.spd.dsccc.com> wrote:
- >> #define FRUIT_LIST \
- >> FRUIT(apple), \
- >> FRUIT(orange), \
- >> FRUIT(pear)
- >>
- >> #define FRUIT(t) t
- >> typedef enum { ITEM_LIST } Fruit;
- >> #undef FRUIT
- >>
- >> // #t produces a quoted string
- >> #define FRUIT(t) #t
- >> static const char* const FruitNameTable[] = { ITEM_LIST };
- >> #undef ITEM
- >>
- >>When the list gets long, techniques like this can be useful for
- >>reducing redundancies in source code. I find that as soon
- >>as I have to specify the same thing twice, and the compiler is unable
- >>to make sure they match, they won't.
- >
-
- >Is the above convenience worth the extra lines of code and comments?
- >Is it worth the extra time future maintainers are going to have to
- >take figuring out this scheme and verifying in their mind that it does
- >what's expected?
-
- To me, it is. I will go to almost any length to avoid having to
- hand-check two copies of the same information for conformance.
-
- Although these techniques are unfamiliar to some programmers,
- they are not that hard to understand. The # and ## preprocessor operators
- were created because they met a real need; maintaining meta-data about
- C++ compile-time objects is one of them.
-
- I do not worry about writing code that is easy for novice C++
- programmers to understand; I write code so it can be maintained with
- minimal effort by experienced programmers, and I write code so that
- correct compilation means a high probability of correct operation. To
- me, the ten minutes that a new programmer spends understanding the
- macro are vastly cheaper than the two or more hours they will spend
- debugging the first time the two lists become unsynchronized and a
- table lookup fails that should have succeeded. I write code for
- telephone switching equipment. A bug detected in the field is very
- expensive in time and lost goodwill. If I give some novice programmer
- a headache, that's good. Now they have learned a new way to produce
- more maintainable code.
-
- Wouldn't it take less time to just copy&paste the
- >enumerator list and use your editor's keystroke record and playback to
- >safely change the items to string literals?
-
- Yes, but it's error prone. I'm an expert Emacs user, and can define
- keyboard macros in my sleep, but it is much better to use the
- compiler to eliminate the possibility of error.
-
- Of course, different
- >programmers will have different opinions on this, but I wouldn't want
- >to use or maintain macro tricks like the above
-
- >(though I've seen much
- >worse -- the "#define GLOBAL extern" trick is especially silly,
- >especially when people get fancy and try to add a INIT(value) macro).
- >
-
- Yes, and it's also totally pointless. In C++, static member functions
- should almost always be used instead of externs.
-
-
- --
- Kevin Cline
-